package chatbot.base.event;

import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.concurrent.ConcurrentSkipListSet;

public abstract class AbstractEvent implements Event {
    private static EventManager em = new SimpleEventManager();
    private SortedSet<Listener> listeners;
    private static final Comparator<Listener> compare = new Comparator<Listener>() {
        public int compare(Listener i, Listener j) {
            int result = i.getPriority().compareTo(j.getPriority());
            return result;
        }
    };

    public abstract String getName();

    public void registerListener(Listener l) {
        if (listeners != null) {
            listeners.add(l);
        } else {
            listeners = new ConcurrentSkipListSet(compare);
            listeners.add(l);
        }
    }
    public void alertListeners() {
        if (listeners != null && listeners.size() > 0) {
            Iterator<Listener> i = listeners.iterator();
            while(i.hasNext()) {
                Listener l = i.next();
                l.execute(this);
            }
        }
    }
    public static EventManager getEventManager() {
        return em;
    }
}
